home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17395 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: mudskipper.cac.psu.edu!user
  2. From: fcusack@tdx.org (frank.)
  3. Newsgroups: comp.lang.c++,comp.lang.c,comp.os.msdos.programer,comp.os.ms-windows.programmer.misc
  4. Subject: Re: fastest code
  5. Date: Mon, 15 Apr 1996 14:35:17 -0400
  6. Organization: Soylent Green is People!!
  7. Message-ID: <fcusack-1504961435170001@mudskipper.cac.psu.edu>
  8. References: <316112A2.7D37@public.sta.net.cn> <31611AC9.7DE1@wight.hursley.ibm.com> <3162c21a.6084138@204.248.25.97> <31641DE2.31D4@halcyon.com> <31658942.99299605@204.248.25.97> <4kp41m$jhc@news.manawatu.gen.nz>
  9. NNTP-Posting-Host: mudskipper.cac.psu.edu
  10.  
  11. In article <4kp41m$jhc@news.manawatu.gen.nz>, gthomas@manawatu.gen.nz wrote:
  12.  
  13. > srwillrd@novia.net (William E. Kempf) wrote:
  14. > >As an example:  if the C code looked like this...
  15. > >while (condition)
  16. > >{
  17. > >   a = 10;
  18. > >   // other code that does not change a
  19. > >}
  20. > >a good optimizing compiler would move the a = 10 statement before the
  21. > >loop in the final machine code. 
  22. > How about this sequence:
  23. > b = 6;
  24. > a = 5;
  25. > while (b < 4)
  26. > {
  27. >   a = 10;
  28. >   // other code that does not affect q
  29.                                       ^^^
  30.  
  31. What is q?
  32.  
  33. > }
  34. > Your "optimizing" compiler would come out of that one with a == 10,
  35. > when it should be a == 5
  36.  
  37. No, it should be a == 10. The 'a' inside the loop is the same as the one
  38. outside the loop because you did not declare a new variable. IF, on the
  39. other hand, you had written:
  40.  
  41. b = 6;
  42. a = 5;
  43. while (b < 4) {
  44.    int a = 10;
  45.    // code that does not affect either a
  46. }
  47.  
  48. the "optimizing" compiler will still move a outside the loop, but when the
  49. loop is exited, a will again be 5. ie the compiler will treat it as if the
  50. inner a were a totally different variable, as it is.
  51.  
  52. ~Frank
  53.  - Through the modem, past the router, over the firewall.. nothing but Net -
  54.  -  PGP ID: 1C0F6685 | NCB#56 | Visit me --> http://www.tdx.org/~fcusack/  -
  55.